﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Seqex.Tests;
using System.Collections;

namespace MicroBenchmarks.GraphTest
{
    public class AdjacencyGraph
    {
        const int MAX_VERTICES = 10;
        const int MAX_EDGES = 5;
        int vertexCount = 0;
        private Vertex[] vertexSet;

        int edgeCount = 0;
        private Edge[] edgeSet;

        public int EdgeSetCnt
        {
            get
            {
                return edgeCount;
            }
        }

        public Edge[] EdgeSet
        {
            get
            {
                return edgeSet;
            }
        }

        public AdjacencyGraph()
        {
            this.vertexSet = new Vertex[MAX_VERTICES];
            this.edgeSet = new Edge[MAX_EDGES];
        }

        public void AddVertex(Vertex v)
        {
            //if (this.vertexCount == MAX_VERTICES)
            //    throw new Exception("Vertices full");

            if (v == null)
                return;

            this.vertexSet[this.vertexCount++] = v;            
        }

        private bool ContainsVertex(Vertex v)
        {
            foreach (var vin in this.vertexSet)
            {
                if (vin == v)
                    return true;
            }

            return false;
        }

        public Edge AddEdge(Vertex v1, Vertex v2)
        {
            //if (this.edgeCount == MAX_EDGES)
            //    throw new Exception("Edges full");
            if (this.vertexCount <= 1)
                return null;

            if (v1 == null || v2 == null)
                throw new Exception("Vertices are empty");
                        
            if (!this.ContainsVertex(v1))
                return null;

            if (!this.ContainsVertex(v2))
                return null;

            if (v1 == v2)
                return null;

            Edge edge = new Edge();
            edge.SetStart(v1);
            edge.SetEnd(v2);
            this.edgeSet[this.edgeCount++] = edge;
            return edge;
        }
    }
}
